#include "qmycombobox.h"

#include "../../include/qcommonstruct.h"
#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"

#include <QVariant>
#include <QMouseEvent>

QComboBoxHost::QComboBoxHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QComboBoxHost::currentIndex()
{
    return ((QComboBox*)m_widget)->currentIndex();
}
QString QComboBoxHost::currentText()
{
    return ((QComboBox*)m_widget)->currentText();
}

void QComboBoxHost::removeIndex(int index)
{
    ((QComboBox*)m_widget)->removeItem(index);
}

void QComboBoxHost::insertIndex(int index, const QString &text)
{
    ((QComboBox*)m_widget)->insertItem(index,text);
}

int QComboBoxHost::count()
{
    return ((QComboBox*)m_widget)->count();
}

QMyComboBox::QMyComboBox(QWidget *parent):
    QComboBox(parent),
    m_host(new QComboBoxHost(this))
{
    setProperty("className","QComboBox");
    setProperty("typeName","Combo Box");
    setProperty("typeIcon",":/images/widgets/combobox.png");

    m_host->addProperty("Attribute",QStringList()<<"font"<<"focusPolicy"<<"comboBox_items"
                        <<"currentIndex"<<"maxVisibleItems"<<"editable");
    m_host->addProperty("Signals",QStringList()<<"currentIndexChanged");
    m_host->addUserProperty(QStringList()<<"currentIndex");

    connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(current(int)));
}

void QMyComboBox::current(int index)
{
    QString code=property("currentIndexChanged").toString();
    if(code!="")
    {
        QString para=QString("type=currentIndexChanged,num=%1,_index=%2").arg(1).arg(index);
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="currentIndex")
            {
                manager->setUserData(info.m_valueName,index);

            }
        }
    }
}

void QMyComboBox::updateProperty(const QString &name)
{
    PropertyList list=property("property_list").value<PropertyList>();
    if(name=="comboBox_items")
    {
        QVariant v=property("comboBox_items");
        ComboItems items=v.value<ComboItems>();
        this->clear();

        foreach(tagComboItem item,items)
        {
            addItem(QIcon(item.m_icon),item.m_text);
        }
        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="currentIndex")
            {
                p->setAttribute("min",-1);
                p->setAttribute("max",this->count()-1);
            }
        }
    }
    else if(name=="Z-Value")
    {
        emit zValueChanged();
    }
}

void QMyComboBox::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="currentIndex")
        {
            p->setAttribute("min",-1);
            p->setAttribute("max",this->count());
        }
        else if(name=="maxVisibleItems")
        {
            p->setAttribute("min",1);
            p->setAttribute("max",255);
        }
        else if(name=="currentIndexChanged")
        {
            p->setAttribute("param","currentIndexChanged");
        }
    }
}

void QMyComboBox::initProperty()
{
    disconnect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(current(int)));
    QVariant v=property("comboBox_items");
    ComboItems items=v.value<ComboItems>();

    foreach(tagComboItem item,items)
    {
        addItem(QIcon(item.m_icon),item.m_text);
    }
    connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(current(int)));
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}

